/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;

import com.asseco.mio.BasicInfo;
import com.asseco.mio.UserInstitution;
import com.asseco.mio.WorkPosition;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import com.asseco.mio.search.UserSearch;
import com.asseco.persistence.service.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.model.Role;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;
import org.appfuse.service.UserManager;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author gjoko.mitrovski
 */
@Controller
@RequestMapping("/userinstitutionsearch*")
public class UserInstitutionSearchController extends BaseFormController {

    @Autowired
    UserManager userManager;

    public List getUsers(User user) {

        if (user.getFirstName() == null && user.getLastName() == null && user.getUsername() == null) {
            return null;
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String pamUsername = "%";

        if (!user.getUsername().equals("")) {
            pamUsername = "%" + user.getUsername() + "%";
        }
        Criteria criteria = session.createCriteria(User.class).add(Restrictions.like("username", pamUsername));

        List result = criteria.list();
        session.getTransaction().commit();
        for (int i = 0; i < result.size(); i++) {
            User tempUser = (User) result.get(i);
            if (!tempUser.getRoleList().toString().contains("ROLE_USER")) {
                result.remove(i);
            }
        }
        return result;

    }

    public List getMioUsers(User user) {

        if (user.getFirstName() == null && user.getLastName() == null && user.getUsername() == null) {
            return null;
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String pamUsername = "%";

        if (!user.getUsername().equals("")) {
            pamUsername = "%" + user.getUsername() + "%";
        }
        Criteria criteria = session.createCriteria(User.class).add(Restrictions.like("username", pamUsername));

        List<User> result = criteria.list();
        List<User> result1 = new ArrayList<User>();
        session.getTransaction().commit();
        for (int i = 0; i < result.size(); i++) {
            User tempUser = (User) result.get(i);
            if (tempUser.getRoleList().toString().contains("ROLE_MIO")) {
                result1.add(tempUser);
            }
        }
        return result1;

    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView doSubmit(User user, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.addObject("userinstitutionsearchlist", getUsers(user));
        mav.addObject("miouserinstitutionsearchlist", getMioUsers(user));
        mav.setViewName("userinstitutionsearch");
        return mav;
    }
}
